<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/6 0006
 * Time: 15:32
 */

namespace app\index\controller;

use think\facade\Request;
use think\Controller;

class Store extends Controller
{
    public function index(){
        $m_id = session('member_id');
        if(!empty($m_id)){
            $member = db('member')->where('id',$m_id)->find();
            $this->assign('member',$member);
        }
        $getCateId = input('getCateId') ?? '1-0-0-0';
        //获取第一条数据
        $firstData = $this->firstData();
        $arr = explode('-',$getCateId);
        $cateByData = $this->cateByData($arr[0]);
        $selectData = $this->selectData($arr);
        $currentPage = $selectData->currentPage();
        $lastPage = $selectData->lastPage();
        $footer = $this->footer();
        if(input('page') > $lastPage){
            $this->error("改页码不存在","/index/store/index");
        }
//        echo $currentPage;
//        dump($selectData);die;
        $this->assign([
            'current' => 'store',
            'getCateId' => $getCateId,
            'arr' => $arr,
            'firstData' => $firstData,
            'cateByData' => $cateByData,
            'selectData' => $selectData,
            'lastPage' => $lastPage,
            'currentPage' => $currentPage,
            'footer' => $footer
        ]);
        return view("index");
    }
//    查询出第一梯队类别
    public function firstData(){
        $first = db("cate")->cache(900)->where('pid',0)->select();
        return $first;
    }

//    根据类别id调用数据
    public function cateByData($pid){
        $data = db("cate")->where('pid',$pid)->select();
        return $data;
    }
//    查询数据
    public function selectData($arr){
        $map = [];
        $map['cate_pid'] = $arr[0];
        if($arr[1] != 0) $map['cate_id'] = $arr[1];
        if($arr[2] == 'yes') $map['complete'] = 1;
        if($arr[2] == 'no') $map['complete'] = 0;
//        if($arr[3] == '') $map['']
//        dump($map);die;
        $data = db("book")->where($map)->where('state',1)->paginate(8);
        return $data;
    }
    public function footer(){
        $data = db("web") ->cache(true,600)->find();
        return $data;
    }
//    public function bookCate(){
//        $first = db("cate")->cache(360)->where('pid',0)->select();
//        $male = db("cate")->cache(360)->where('pid',1)->select();
//        $female = db("cate")->cache(360)->where('pid',2)->select();
//        $publish = db("cate")->cache(360)->where('pid',3)->select();
//        $secondary = db("cate")->cache(360)->where('pid',4)->select();
//        return [
//            'first' => $first,
//            'male' => $male,
//            'female' => $female,
//            'publish' => $publish,
//            'secondary' => $secondary
//        ];
//    }
}